﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using APP.HELPER.Utils.Language;
using DevExpress.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.Views.Grid;
using Dictionary.ERP;
using Common;
using Common.Class;
using Extra.ERP;
using Security.ERP;

namespace Dictionary
{
    public class xucUnit : xucBaseBasic
    {
        private IContainer components;

        public xucUnit()
        {
            InitializeComponent();
            base.ucToolBar.SetInterface();
        }

        protected override void Add()
        {
            var add = new xfmUnitAdd(Actions.Add);
            add.Added += frm_Added;
            add.ShowDialog();
        }

        private void AddRow(DIC_UNIT Item)
        {
            AdvBandedGridView gbList = base.gbList;
            int focusedRowHandle = gbList.FocusedRowHandle;
            gbList.AddNewRow();
            focusedRowHandle = gbList.FocusedRowHandle;
            gbList.SetRowCellValue(focusedRowHandle, "Active", Item.Active);
            gbList.SetRowCellValue(focusedRowHandle, "UnitCode", Item.UnitCode);
            gbList.SetRowCellValue(focusedRowHandle, "UnitName", Item.UnitName);
            gbList.SetRowCellValue(focusedRowHandle, "Description", Item.Description);
            gbList.UpdateCurrentRow();
        }

        public override void Change()
        {
            var item = new DIC_UNIT();
            object cellValue = base.GetCellValue(base.m_RowClickEventArgs.RowIndex, "UnitCode");
            if (cellValue != null)
            {
                base.SetWaitDialogCaption("Đang kiểm tra dữ liệu....");
                if (item.Get(cellValue.ToString()) != "OK")
                {
                    this.DoHide();
                    XtraMessageBox.Show("Dữ liệu kh\x00f4ng tồn tại", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation);
                }
                else
                {
                    this.DoHide();
                    var add = new xfmUnitAdd(Common.Class.Actions.Update, item);
                    add.Updated += frm_Updated;
                    add.Added += frm_Added;
                    add.ShowDialog();
                }
            }
        }

        public override void Delete()
        {
            if (!ClsOption.System2.IsQuestion ||
                (XtraMessageBox.Show("Bạn c\x00f3 muốn x\x00f3a kh\x00f4ng?", "Th\x00f4ng b\x00e1o",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No))
            {
                object cellValue;
                base.SetWaitDialogCaption("Đang x\x00f3a...");
                string str = "";
                bool flag = false;
                AdvBandedGridView gbList = base.gbList;
                int[] selectedRows = gbList.GetSelectedRows();
                var dic_unit = new DIC_UNIT();
                for (int i = selectedRows.Length; i > 0; i--)
                {
                    flag = true;
                    cellValue = base.GetCellValue(selectedRows[i - 1], "UnitCode");
                    if (cellValue != null)
                    {
                        SYS_LOG.Insert("Danh Mục Đơn Vị T\x00ednh", "Xo\x00e1", cellValue.ToString());
                        str = dic_unit.Delete(cellValue.ToString());
                        if (str == "OK")
                        {
                            gbList.DeleteRow(selectedRows[i - 1]);
                        }
                        else if (str != "OK")
                        {
                            MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\n" + str, "Th\x00f4ng b\x00e1o",
                                MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                    }
                }
                this.DoHide();
                if (!flag && (gbList.DataSource != null))
                {
                    var args = new Common.Class.RowClickEventArgs((gbList == null) ? -1 : gbList.FocusedRowHandle,
                        (gbList.FocusedColumn == null) ? -1 : gbList.FocusedColumn.ColumnHandle,
                        (gbList.FocusedColumn == null) ? "" : gbList.FocusedColumn.FieldName);
                    base.m_RowClickEventArgs = args;
                    cellValue = null;
                    cellValue = base.GetCellValue(args.RowIndex, "UnitCode");
                    if (cellValue != null)
                    {
                        SYS_LOG.Insert("Danh Mục Đơn Vị T\x00ednh", "Xo\x00e1", cellValue.ToString());
                        base.SetWaitDialogCaption("Đang x\x00f3a...");
                        str = dic_unit.Delete(cellValue.ToString());
                        if (str == "OK")
                        {
                            gbList.DeleteRow(args.RowIndex);
                        }
                        else if (str != "OK")
                        {
                            MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\n" + str, "Th\x00f4ng b\x00e1o",
                                MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                        this.DoHide();
                    }
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override bool ExportPermision()
        {
            SYS_LOG.Insert("Danh Mục Đơn Vị T\x00ednh", "Xuất");
            return base.ExportPermision();
        }

        private void frm_Added(object sender, DIC_UNIT e)
        {
            AddRow(e);
        }

        private void frm_Updated(object sender, DIC_UNIT Item)
        {
            UpdateRow(Item, base.m_RowClickEventArgs);
        }

        protected override void gbList_MouseDown(object sender, MouseEventArgs e)
        {
            var view = (AdvBandedGridView) sender;
            var args = new Common.Class.RowClickEventArgs((view == null) ? -1 : view.FocusedRowHandle,
                (view.FocusedColumn == null) ? -1 : view.FocusedColumn.ColumnHandle,
                (view.FocusedColumn == null) ? "" : view.FocusedColumn.FieldName);
            base.m_RowClickEventArgs = args;
            object cellValue = base.GetCellValue(args.RowIndex, "UnitCode");
            base.DisableMenu(false);
            if (cellValue == null)
            {
                base.DisableMenu(true);
            }
        }

        private void InitializeComponent()
        {
            components = new Container();
            base.AutoScaleMode = AutoScaleMode.Font;
        }

        protected override void List_Init(AdvBandedGridView dt)
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                string fieldName = dt.Columns[i].FieldName;
                if (fieldName == null)
                {
                    goto Label_0275;
                }
                if (!(fieldName == "UnitCode"))
                {
                    if (fieldName == "UnitName")
                    {
                        goto Label_0108;
                    }
                    if (fieldName == "Description")
                    {
                        goto Label_01C0;
                    }
                    goto Label_0275;
                }
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = MultiLanguages.GetString("tbl_FormBasic", "ID", "M\x00e3");
                }
                dt.Columns[i].Width = 60;
                continue;
                Label_0108:
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = MultiLanguages.GetString("tbl_FormBasic", "Name", "T\x00ean");
                }
                dt.Columns[i].Width = 180;
                continue;
                Label_01C0:
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = MultiLanguages.GetString("tbl_FormBasic", "Description", "Ghi ch\x00fa");
                }
                dt.Columns[i].Width = 150;
                continue;
                Label_0275:
                dt.Columns[i].Visible = false;
            }
        }

        protected override void Print()
        {
            SYS_LOG.Insert("Danh Mục Đơn Vị T\x00ednh", "In");
            base.Print();
        }

        public override void ReLoad()
        {
            base.SetWaitDialogCaption("Đang nạp dữ liệu...");
            base.gcList.DataSource = new DIC_UNIT().GetList();
            base.SetWaitDialogCaption("Đang nạp cấu h\x00ecnh...");
            List_Init(base.gbList);
            base.SetWaitDialogCaption("Nạp quyền sử dụng...");
            MyRule.Get(MyLogin.RoleId, "bbiUnit");
            if (!MyRule.AllowPrint)
            {
                base.ucToolBar.bbiPrint.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiUnit");
            if (!MyRule.AllowExport)
            {
                base.ucToolBar.bbiExport.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiUnit");
            if (!MyRule.AllowAdd)
            {
                base.ucToolBar.bbiAdd.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiUnit");
            if (!MyRule.AllowDelete)
            {
                base.ucToolBar.bbiDelete.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiUnit");
            if (!MyRule.AllowEdit)
            {
                base.ucToolBar.bbiEdit.Visibility = BarItemVisibility.Never;
            }
            base.SetWaitDialogCaption("Đ\x00e3 xong...");
            this.DoHide();
        }

        private void UpdateRow(DIC_UNIT item, Common.Class.RowClickEventArgs e)
        {
            AdvBandedGridView gbList = base.gbList;
            int rowIndex = e.RowIndex;
            gbList.SetRowCellValue(rowIndex, "Active", item.Active);
            gbList.SetRowCellValue(rowIndex, "UnitCode", item.UnitCode);
            gbList.SetRowCellValue(rowIndex, "UnitName", item.UnitName);
            gbList.SetRowCellValue(rowIndex, "Description", item.Description);
            gbList.UpdateCurrentRow();
        }

        private void xucCustomerType_Load(object sender, EventArgs e)
        {
        }
    }
}